﻿Public Class frmParametriInstallazione

    Dim strNameForm As String = "Impostazione parametri installazione"

    Public Function Initialize() As Boolean
        Try
            Me.Text = strNameForm

            Dim blnServer As Boolean = GetSetting(Application.ProductName, "Impostazioni", "Server", 1)
            If blnServer Then
                optServer.Checked = True
                txtPathMySqlDump.Text = GetSetting(Application.ProductName, "Impostazioni", "PathMySqlDump", "")
            Else
                optClient.Checked = True
                txtIndirizzoIP.Text = GetSetting(Application.ProductName, "Impostazioni", "IndirizzoIP", "")
            End If
            txtIstanza.Text = GetSetting(Application.ProductName, "Impostazioni", "Istanza", "")
            txtPathReport.Text = GetSetting(Application.ProductName, "Impostazioni", "PathReport", "")

            Return True
        Catch ex As Exception
            Base.ShowError.Show("Initialize", Me.Name, "Initialize", ex)
            Return False
        End Try
    End Function

#Region "Commands"

    Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    Private Sub cmdSalva_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSalva.Click
        Try
            If optClient.Checked And txtIndirizzoIP.Text = "" Then
                MsgBox("Inserire l'indirizzo IP del server.", MsgBoxStyle.Exclamation, Me.Text)
                Return
            End If

            SaveSetting(Application.ProductName, "Impostazioni", "Server", If(optServer.Checked, 1, 0))
            SaveSetting(Application.ProductName, "Impostazioni", "IndirizzoIP", txtIndirizzoIP.Text)
            SaveSetting(Application.ProductName, "Impostazioni", "Istanza", txtIstanza.Text)
            SaveSetting(Application.ProductName, "Impostazioni", "PathMySqlDump", txtPathMySqlDump.Text)
            SaveSetting(Application.ProductName, "Impostazioni", "PathReport", txtPathReport.Text)

            Call Utility.CaricaImpostazioni()

            Me.DialogResult = Windows.Forms.DialogResult.OK
        Catch ex As Exception
            Base.ShowError.Show("Avvia conversione", Me.Name, "cmdSalva_Click", ex)
        End Try
    End Sub

    Private Sub cmdPathMySqlDump_OnClickOrPress(ByRef sender As Base.SuperSmallButton) Handles cmdPathMySqlDump.OnClickOrPress
        Try
            Dim oFolderBrowserDialog As New FolderBrowserDialog
            oFolderBrowserDialog.RootFolder = Environment.SpecialFolder.Desktop
            oFolderBrowserDialog.Description = "Selezionare la cartella."
            If oFolderBrowserDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
                txtPathMySqlDump.Text = oFolderBrowserDialog.SelectedPath
            End If
            oFolderBrowserDialog = Nothing
        Catch ex As Exception
            Base.ShowError.Show("Seleziona percorso", Me.Name, "cmdPath_OnClickOrPress", ex)
        End Try
    End Sub

    Private Sub cmdApriPathReport_OnClickOrPress(ByRef sender As Base.SuperSmallButton) Handles cmdApriPathReport.OnClickOrPress
        Try
            Dim oFolderBrowserDialog As New FolderBrowserDialog
            oFolderBrowserDialog.RootFolder = Environment.SpecialFolder.Desktop
            oFolderBrowserDialog.Description = "Selezionare la cartella."
            If oFolderBrowserDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
                txtPathReport.Text = oFolderBrowserDialog.SelectedPath
            End If
            oFolderBrowserDialog = Nothing
        Catch ex As Exception
            Base.ShowError.Show("Seleziona percorso", Me.Name, "cmdApriPathReport_OnClickOrPress", ex)
        End Try
    End Sub

#End Region

#Region "Form events"

    Private Sub me_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Escape Then
            Me.DialogResult = DialogResult.Cancel
        End If
    End Sub

#End Region

End Class